// pages/home/index.js
const http = require('../../utils/http.js');
import regeneratorRuntime from '../../utils/runtime.js'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    keyword: '',
    type: null,
    currentPage: 1,
    pageSize: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    /* 建议放在onReady里执行，提前加载广告 */
    let InterstitialAd = qq.createInterstitialAd({ adUnitId: '57eca5ef0872ed8ab5d5b310f71db977' });
    InterstitialAd.load().catch((err) => { console.error('load', err) })
    InterstitialAd.onLoad(() => { console.log('onLoad event emit') })
    InterstitialAd.onClose(() => { console.log('close event emit') })
    InterstitialAd.onError((e) => { console.log('error', e) })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      list: [],
      currentPage: 1,
    })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    /* 建议放在需要展示插屏广告的时机执行 */
    let InterstitialAd = qq.createInterstitialAd({ adUnitId: '57eca5ef0872ed8ab5d5b310f71db977' });
    InterstitialAd.show().catch((err) => { console.error('show', err) })
    this.setData({
      currentPage: this.data.currentPage + 1
    })
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
          title: "拼车信息免费发，快来戳我吧~",
          path: '/pages/home/index',
          imageUrl: '../../image/share.jpg'
     }
  },
  go: function (e) {
    wx.navigateTo({
      url: '/pages/publish/order/order?id=' + e.currentTarget.dataset.id,
    })
  },
  input: function (e) {
    this.setData({
      keyword: e.detail
    });
  },
  getList: async function () {
    wx.showLoading({
      title: '加载中...',
    })
    var that = this;
    var res = await http.get('/carpool/getlist', {
      keyword: that.data.keyword,
      type: that.data.type,
      pageindex: that.data.currentPage,
      pagesize: that.data.pageSize,
    });
    this.setData({
      list: that.data.list.concat(res.result)
    })
    wx.hideLoading();
  },
  search: function () {
    this.setData({
      currentPage: 1,
      list: [],
    })
    this.getList();
  },
  golist: function (e) {
    wx.navigateTo({
      url: '/pages/home/list/list?type=' + e.currentTarget.dataset.id,
    })
  }
})